import django.http
from django.shortcuts import render

# Create your views here.
from django.shortcuts import  render

from django.views import  View
from django.http.response import HttpResponseBadRequest
from libs.captcha.captcha import captcha
from django_redis import get_redis_connection
from django.http import  HttpResponse
class RegisterView(View):
    def get(self, request):
        return render(request, 'register.html')

class ImageCodeView(View):
    def get(self, request):
        # 接收前端参数uuid
        uuid = request.GET.get('uuid')
        # 判断uuid是否获取到
        if uuid is None:
            return HttpResponseBadRequest('uuid为空')
        # 通过调用captcha生成图片验证码
        text,image =  captcha.generate_captcha()
        # 将图片内容保存到redis中
        redis_conn = get_redis_connection('default')
        redis_conn.set('img:%s'%uuid,text,300)
        # 将图片返回
        return HttpResponse(image,content_type='image/jpeg')
